﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.Shell;
using System.Threading;

namespace PhotoPuzzle
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();
            GameGlobal.Random = new Random((int)DateTime.Now.Ticks);
            foreach (var item in menuPanel.Children)
            {
                item.MouseLeftButtonDown += new MouseButtonEventHandler(item_MouseLeftButtonDown);
            }
            (menuPanel.Children[0] as MenuButton).OnActionBegin = StartGame;
            (menuPanel.Children[1] as MenuButton).OnActionBegin = SelectPictrue;
            (menuPanel.Children[2] as MenuButton).OnActionBegin = AboutMe;
            foreach (var item in pictrues.Children)
            {
                item.MouseLeftButtonDown+=new MouseButtonEventHandler(pictrues_MouseLeftButtonDown);
            }
            PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            Loaded -= MainPage_Loaded;
            setLanguage(GameGlobal.Language);
            if (GameGlobal.Language == EmLanguage.CN)
                cnCheckButton.IsChecked = true;
            else
                enCheckButton.IsChecked = true;
        }
        string[] cnbuttonimgs = new string[3]
        {
            "/PhotoPuzzle;component/Res/cn/start.png",
            "/PhotoPuzzle;component/Res/cn/select.png",
            "/PhotoPuzzle;component/Res/cn/about.png"
        };
        string[] enbuttonimgs = new string[3]
        {
            "/PhotoPuzzle;component/Res/en/start.png",
            "/PhotoPuzzle;component/Res/en/select.png",
            "/PhotoPuzzle;component/Res/en/about.png"
        };
        void enCheckButton_Checked(object sender, RoutedEventArgs e)
        {
            if (menuPanel == null)
                return;
            GameGlobal.Language = EmLanguage.EN;
            setLanguage(EmLanguage.EN);
        }

        void cnCheckButton_Checked(object sender, RoutedEventArgs e)
        {
            if (menuPanel == null)
                return;
            GameGlobal.Language = EmLanguage.CN;
            setLanguage(EmLanguage.CN);
        }
        void setLanguage(EmLanguage l)
        {
            switch (l)
            {
                case EmLanguage.CN:
                    //中文
                    for (int i = 0; i < menuPanel.Children.Count; i++)
                    {
                        (menuPanel.Children[i] as MenuButton).ButtonImageSource = new BitmapImage(new Uri(cnbuttonimgs[i], UriKind.Relative));
                    }
                    break;
                case EmLanguage.EN:
                    //英文
                    for (int i = 0; i < menuPanel.Children.Count; i++)
                    {
                        (menuPanel.Children[i] as MenuButton).ButtonImageSource = new BitmapImage(new Uri(enbuttonimgs[i], UriKind.Relative));
                    }
                    break;
                default:
                    break;
            }
            
            
        }
        Size[] Levelgroup = new Size[]
        {
            new Size(2,2),
            new Size(3,2),
            new Size(4,3),
            new Size(5,3),
            new Size(6,4),
            new Size(7,4),
        };
        void pictrues_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if ((sender as Pictrue).IsNull)
                return;
            foreach (Pictrue item in pictrues.Children)
                item.IsSelect = false;
            (sender as Pictrue).IsSelect = true;
            var i = pictrues.Children.IndexOf(sender as Pictrue);
            if (i >= Levelgroup.Length)
                i = Levelgroup.Length - 1;
            GameGlobal.SetLevel(Levelgroup[i]);
            GameGlobal.PictrueSource = (sender as Pictrue).ButtonImageSource;
        }
        void item_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var s = (sender as MenuButton);
            if (s == null)
                return;
            if (s.IsSelect)
            {
                if(s.OnActionBegin!=null)
                    s.OnActionBegin();
            }
            else
            {
                foreach (MenuButton item in menuPanel.Children)
                    item.IsSelect = false;
                s.IsSelect = true;
            }
        }
        public void StartGame()
        {
            if (GameGlobal.PictrueSource == null)
            {
                GameGlobal.SetLevel(Levelgroup[0]);
                GameGlobal.PictrueSource = (pictrues.Children[0] as Pictrue).ButtonImageSource;
            }
            this.NavigationService.Navigate(new Uri("/PhotoPuzzle;component/GameInterface.xaml", UriKind.Relative));
        }
        public void AboutMe()
        {
            aboutMe.Visibility = System.Windows.Visibility.Visible;
        }
        public void SelectPictrue()
        {
            PhotoChooserTask photoChooserTask = new PhotoChooserTask();
            photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
            photoChooserTask.ShowCamera = true;
            photoChooserTask.Show();
        }

        void photoChooserTask_Completed(object sender, PhotoResult e)
        {
            (sender as PhotoChooserTask).Completed -= photoChooserTask_Completed;
            if (e.TaskResult != TaskResult.OK)
                return;
            BitmapImage bt = new BitmapImage();
            bt.SetSource(e.ChosenPhoto);
            GameGlobal.PictrueSource = bt;
            //GameGlobal.PhotoStream = e.ChosenPhoto;
            this.Dispatcher.BeginInvoke(() =>
                {
                    GameGlobal.SetLevel(5, 3);
                    StartGame();
                });
            
        }
        
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (aboutMe.Visibility != System.Windows.Visibility.Collapsed)
            {
                aboutMe.Visibility = System.Windows.Visibility.Collapsed;
                e.Cancel = true;
            }
            base.OnBackKeyPress(e);
        }
        private void aboutme_buttonback(object sender, System.Windows.RoutedEventArgs e)
        {
            aboutMe.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}
